<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书馆管理系统 - 注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', 'PingFang SC', Arial, sans-serif;
        }

        body {
            /* 与登录页一致的渐变背景 */
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        /* 注册容器，与登录页样式统一 */
        .container {
            background: rgba(255, 255, 255, 0.95);
            padding: 50px 40px;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(31, 38, 135, 0.2);
            backdrop-filter: blur(4px);
            border: 1px solid rgba(255, 255, 255, 0.18);
            width: 100%;
            max-width: 450px;
        }

        /* 标题样式，与登录页统一 */
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 35px;
            font-size: 24px;
            position: relative;
        }

        h1::after {
            content: '';
            display: block;
            width: 50px;
            height: 3px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            margin: 10px auto 0;
            border-radius: 3px;
        }

        /* 表单行容器：实现一行两列 */
        .form-row {
            display: flex;
            gap: 10px;
            margin-bottom: 22px;
        }

        /* 表单组样式（一行两列时占比50%） */
        .form-group {
            flex: 1;
            position: relative;
        }

        /* 单独一行的表单组（如验证码） */
        .form-group.full-width {
            width: 100%;
            margin-bottom: 22px;
        }

        /* 错误提示样式优化，与登录页统一 */
        .error-message {
            color: #dc3545;
            font-size: 13px;
            margin-top: 6px;
            display: block;
            text-align: left;
            padding-left: 2px;
        }

        /* 输入框样式，与登录页统一 */
        input {
            width: 100%;
            padding: 7px 10px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            font-size: 15px;
            transition: all 0.3s ease;
            background: rgba(255, 255, 255, 0.8);
        }

        /* 性别单选框组样式 */
        .gender-group {
            display: flex;
            align-items: center;
            gap: 15px;
            padding: 5px 10px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            background: rgba(255, 255, 255, 0.8);
        }

        .gender-group input {
            width: auto;
            margin-right: 5px;
            padding: 0;
        }

        .gender-group label {
            color: #555;
            cursor: pointer;
        }

        input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
        }

        /* 错误输入框样式 */
        input.error {
            border-color: #dc3545;
        }

        .gender-group.error {
            border-color: #dc3545;
        }

        input.error:focus {
            box-shadow: 0 0 0 3px rgba(220, 53, 69, 0.15);
        }

        /* 注册按钮样式，与登录页统一 */
        button {
            width: 100%;
            padding: 14px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: all 0.3s ease;
            letter-spacing: 0.5px;
            margin-top: 10px;
        }

        button:hover {
            background: linear-gradient(90deg, #5a6edb, #6b449a);
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
        }

        button:active {
            transform: translateY(0);
            box-shadow: 0 2px 8px rgba(102, 126, 234, 0.2);
        }

        /* 登录链接区域，与登录页统一 */
        .login-link {
            text-align: center;
            margin-top: 25px;
            font-size: 14px;
            color: #666;
        }

        a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        a:hover {
            color: #764ba2;
            text-decoration: none;
        }

        /* 验证码区域样式优化 */
        .verify-code-group {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        #checkCodeImg {
            height: 46px;
            border-radius: 8px;
            cursor: pointer;
            transition: transform 0.2s ease;
        }

        #checkCodeImg:hover {
            transform: scale(1.05);
        }

        #changeImg {
            white-space: nowrap;
            font-size: 13px;
        }

        /* 响应式调整：小屏幕自动换行 */
        @media (max-width: 480px) {
            .container {
                padding: 40px 25px;
            }

            h1 {
                font-size: 22px;
            }

            /* 小屏幕下一行一列 */
            .form-row {
                flex-direction: column;
                gap: 0;
            }

            input {
                padding: 12px 18px;
                font-size: 14px;
            }

            button {
                padding: 12px;
                font-size: 15px;
            }

            .verify-code-group {
                flex-wrap: wrap;
                justify-content: flex-start;
            }

            #checkCodeImg {
                margin-top: 10px;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <h1>账号注册</h1>
    <form id="registerForm" action="registeredServlet" method="post">
        <%-- 后端错误提示（用户名已存在等） --%>
        <c:if test="${not empty requestScope.error}">
            <div class="error-message">${requestScope.error}</div>
        </c:if>

        <%-- 第一行：用户名 + 性别 --%>
        <div class="form-row">
            <%-- 用户名 --%>
            <div class="form-group">
                <input id="username" type="text" placeholder="请输入用户名（3-12位字母/数字/下划线）" name="username"
                       required>
                <span id="username_err" class="error-message"
                      style="display: none;">用户名格式不正确（3-12位字母/数字/下划线）</span>
            </div>
            <%-- 性别 --%>
            <div class="form-group">
                <div id="genderGroup" class="gender-group">
                    <input type="radio" id="male" name="gender" value="male" required>
                    <label for="male">男</label>
                    <input type="radio" id="female" name="gender" value="female">
                    <label for="female">女</label>
                </div>
                <span id="gender_err" class="error-message" style="display: none;">请选择性别</span>
            </div>
        </div>

        <%-- 第二行：手机号码 + 邮箱 --%>
        <div class="form-row">
            <%-- 电话 --%>
            <div class="form-group">
                <input id="phone" type="tel" placeholder="请输入手机号" name="phone" required>
                <span id="phone_err" class="error-message" style="display: none;">手机号格式不正确（11位数字）</span>
            </div>
            <%-- 邮箱 --%>
            <div class="form-group">
                <input id="email" type="email" placeholder="请输入邮箱" name="email" required>
                <span id="email_err" class="error-message" style="display: none;">邮箱格式不正确</span>
            </div>
        </div>

        <%-- 第三行：密码 + 二次确认密码 --%>
        <div class="form-row">
            <%-- 密码 --%>
            <div class="form-group">
                <input id="password" type="password" placeholder="请输入密码（6-20位字母/数字/下划线）" name="password"
                       required>
                <span id="password_err" class="error-message"
                      style="display: none;">密码格式不正确（6-20位字母/数字/下划线）</span>
            </div>
            <%-- 二次确认密码 --%>
            <div class="form-group">
                <input id="repassword" type="password" placeholder="请再次输入密码" name="repassword" required>
                <span id="repassword_err" class="error-message" style="display: none;">两次密码输入不一致</span>
            </div>
        </div>

        <%-- 第四行：验证码（单独一行，占满宽度） --%>
        <div class="form-group full-width">
            <div class="verify-code-group">
                <input id="checkCode" type="text" placeholder="请输入验证码" name="checkCode" required style="flex: 1;">
                <img id="checkCodeImg" src="checkCodeServlet" alt="验证码">
                <a href="javascript:;" id="changeImg">看不清？</a>
            </div>
            <span id="checkCode_err" class="error-message" style="display: none;">请输入验证码</span>
        </div>

        <button type="submit">立即注册</button>
    </form>
    <div class="login-link">
        已有账号？<a href="index.jsp">前往登录</a>
    </div>
</div>

<script>
    // 刷新验证码
    function refreshVerifyCode() {
        document.getElementById("checkCodeImg").src = "checkCodeServlet?" + new Date().getMilliseconds();
    }

    document.getElementById("checkCodeImg").onclick = refreshVerifyCode;
    document.getElementById("changeImg").onclick = refreshVerifyCode;

    // 输入框元素获取
    const usernameInput = document.getElementById("username");
    const passwordInput = document.getElementById("password");
    const repasswordInput = document.getElementById("repassword");
    const emailInput = document.getElementById("email");
    const phoneInput = document.getElementById("phone");
    const checkCodeInput = document.getElementById("checkCode");
    const genderGroup = document.getElementById("genderGroup");
    const genderRadios = document.getElementsByName("gender");

    // 用户名验证（3-12位字母/数字/下划线）
    usernameInput.onblur = checkUsername;

    function checkUsername() {
        const username = usernameInput.value.trim();
        const reg = /^\w{3,12}$/;
        const flag = reg.test(username);
        document.getElementById("username_err").style.display = flag ? "none" : "block";
        usernameInput.classList.toggle("error", !flag);
        return flag;
    }

    // 性别验证（必填）
    function checkGender() {
        let isChecked = false;
        for (let radio of genderRadios) {
            if (radio.checked) {
                isChecked = true;
                break;
            }
        }
        document.getElementById("gender_err").style.display = isChecked ? "none" : "block";
        genderGroup.classList.toggle("error", !isChecked);
        return isChecked;
    }

    // 密码验证（6-20位字母/数字/下划线）
    passwordInput.onblur = checkPassword;

    function checkPassword() {
        const password = passwordInput.value.trim();
        const reg = /^\w{6,20}$/;
        const flag = reg.test(password);
        document.getElementById("password_err").style.display = flag ? "none" : "block";
        passwordInput.classList.toggle("error", !flag);
        // 密码变化时重新验证二次密码
        checkRepassword();
        return flag;
    }

    // 二次密码验证（与原密码一致）
    repasswordInput.onblur = checkRepassword;

    function checkRepassword() {
        const password = passwordInput.value.trim();
        const repassword = repasswordInput.value.trim();
        const flag = password === repassword && password !== "";
        document.getElementById("repassword_err").style.display = flag ? "none" : "block";
        repasswordInput.classList.toggle("error", !flag);
        return flag;
    }

    // 邮箱验证
    emailInput.onblur = checkEmail;

    function checkEmail() {
        const email = emailInput.value.trim();
        const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        const flag = reg.test(email);
        document.getElementById("email_err").style.display = flag ? "none" : "block";
        emailInput.classList.toggle("error", !flag);
        return flag;
    }

    // 手机号验证（11位数字）
    phoneInput.onblur = checkPhone;

    function checkPhone() {
        const phone = phoneInput.value.trim();
        const reg = /^1[3-9]\d{9}$/;
        const flag = reg.test(phone);
        document.getElementById("phone_err").style.display = flag ? "none" : "block";
        phoneInput.classList.toggle("error", !flag);
        return flag;
    }

    // 验证码验证（非空）
    checkCodeInput.onblur = checkCheckCode;

    function checkCheckCode() {
        const checkCode = checkCodeInput.value.trim();
        const flag = checkCode !== "";
        document.getElementById("checkCode_err").style.display = flag ? "none" : "block";
        checkCodeInput.classList.toggle("error", !flag);
        return flag;
    }

    // 输入框聚焦时清除错误状态
    [usernameInput, passwordInput, repasswordInput, emailInput, phoneInput, checkCodeInput].forEach(input => {
        input.addEventListener("focus", function () {
            this.classList.remove("error");
            document.getElementById(this.id + "_err").style.display = "none";
        });
    });

    // 性别选择时清除错误状态
    for (let radio of genderRadios) {
        radio.addEventListener("change", function () {
            genderGroup.classList.remove("error");
            document.getElementById("gender_err").style.display = "none";
        });
    }

    // 表单提交验证
    document.getElementById("registerForm").onsubmit = function () {
        const isUsernameValid = checkUsername();
        const isGenderValid = checkGender();
        const isPhoneValid = checkPhone();
        const isEmailValid = checkEmail();
        const isPasswordValid = checkPassword();
        const isRepasswordValid = checkRepassword();
        const isCheckCodeValid = checkCheckCode();

        // 所有验证通过才提交
        return isUsernameValid && isGenderValid && isPhoneValid && isEmailValid && isPasswordValid && isRepasswordValid && isCheckCodeValid;
    };
</script>
</body>
</html>
